package com.windigit.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.bobo.mm.CSocket;
import com.bobo.mm.ClientConfig;
import com.bobo.mm.OnCSocketListener;
import com.bobo.mm.SocketFrameworkType;
import com.bobo.mm.dogs.DogFather;
import com.bobo.mm.dogs.DogManager;
import com.bobo.mm.dogs.ErhaDog;
import com.bobo.mm.socket.decoder.LineBasedFrameDecoder;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CSocket.getInstance().init(this, new CSocket.OnServiceListener() {
            @Override
            public void onBindSuccess() {
                Log.i(TAG, "===>onBindSuccess");
                startSocket();
            }

            @Override
            public void onBindError() {
                Log.i(TAG, "===>onBindError");
            }

            @Override
            public void onUnBind() {
                Log.i(TAG, "===>onUnBind");
            }
        });
    }

    private void startSocket() {
        ClientConfig connConfig = new ClientConfig("default");
        connConfig.setFrameworkType(SocketFrameworkType.SOCKET_JAVA_NIO);
//        connConfig.setWebsocketUrl("ws://172.16.11.20:9802");
        connConfig.setWebsocketUrl("ws://47.97.249.212:9802");
        //
        connConfig.setReconnectInterval(2000);
        connConfig.setSocketUrl("192.168.1.4:9802");
        connConfig.setClientId("ws-test");
        connConfig.setDecodeClsName(LineBasedFrameDecoder.class.getName());
        DogManager.getInstance().setDog(new ErhaDog(this)).setDogListener(new DogFather.DogListener() {
            @Override
            public void lookup(int count, int maxCount) {
                Log.i(TAG, "ping====>" + count + "#" + maxCount);
                CSocket.getInstance().sendMessage("ws-test", buildHeartbeat().toString());
            }
        });
        CSocket.getInstance().start(connConfig, new OnCSocketListener() {
            @Override
            public void onOpen(String channelId, byte[] response) {
                Log.i(TAG, "onOpen====>" + channelId);
                DogManager.getInstance().lookup();
            }

            @Override
            public void onMessage(String channelId, String message) {
                Log.i(TAG, "onMessage====>" + channelId + "#" + message);
                DogManager.getInstance().feedDog();
            }

            @Override
            public void onMessage(String clientId, byte[] message) {
                Log.i(TAG, "onMessage====>" + clientId + "#" + new String(message));
                DogManager.getInstance().feedDog();
            }

            @Override
            public void onFailure(String channelId, String exceptionClass, String msg) {
                super.onFailure(channelId, exceptionClass, msg);
                Log.e(TAG, "onFailure====>" + channelId + "#" + exceptionClass + "#" + msg);
            }

            @Override
            public void onReconnecting(String clientId, int count) {
                Log.e(TAG, "onReconnecting====>" + clientId + "#" + count);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DogManager.getInstance().goHome();
        CSocket.getInstance().release();
    }

    private static JSONObject buildHeartbeat() {
        JSONObject object = new JSONObject();
        try {
            object.put("prt", "20000");
            object.put("cmd", "ping");
            object.put("dev", "myTest");
            object.put("tim", Long.toString(System.currentTimeMillis()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }
}
